import { RouteObject } from "../types"
const baseURL = "http://8.142.19.67:8089/api/v1/";
export const useMatchRouter:any = (pathname:string,router_items:RouteObject[]) => {
   for(let item of router_items){
    if(item.meta?.key===pathname)
        return item
    if(item?.children&&item.children.length)
       return useMatchRouter(pathname,item.children)
   }
}
export const request=({
    url,
    method='get',
    headers={"Content-Type":"application/json"},
    body
}:any)=>{
    let options:any;
    if(method.toUpperCase()==='GET')
        options=undefined;
    else{
        options = {
            headers,
            method,
            body:JSON.stringify(body)
        }
    }
    return new Promise((resolve,reject) => {
        fetch(baseURL+url,options).then(response => response.json())
        .then((res)=>{
          resolve(res.data)
        }).catch(err=>reject(err))
    })
            
}
    
